#ifndef TEXTBOX_H
#define TEXTBOX_H

#include "widget.h"
#include "../../image_utils.h"
#include "../main_widget.h"

#include <QTextEdit>

/* assumptions:
   - textbox not resizable
  */

class TextBox: public Widget
{
public:
    TextBox(Widget* _parent, Engine* _engine, int _x, int _y, int _w, int _h,
            MainWidget* _mw, const QFont& _font = QFont("Palatino", 12))
        :Widget(_parent, _engine, _x, _y, _w, _h)
    {
        toHide = false;
        textToSet = false;
        alignment = Qt::AlignLeft;
        edit = new QTextEdit("Prova text box", _mw);
        edit->setGeometry(-1000, -1000, w, h);
        edit->setFont(_font);
        edit->show();
    }

    virtual ~TextBox(){}

    virtual void Paint(QPainter &painter){}

    // this method is invoked by the main gui thread
    virtual void DoQWidgetOperations( void )
    {
        if(toHide)
        {
            edit->setGeometry(-1000, -1000, w, h);
            toHide = false;
            hiddenNotification();
        }

        if( textToSet )
        {
            edit->setText( tempText );
            edit->setAlignment( alignment );
            textToSet = false;
        }

        if(visible)
        {
            edit->setGeometry(x, y, w, h);
        }
    }

    virtual void doHide()
    {
        visible = false;
        toHide = true;
    }

    void SetText( const QString& text )
    {
        tempText = text;
        textToSet = true;
    }

    void SetAlignment( Qt::Alignment _alignment )
    {
        alignment = _alignment;
    }

    QString GetText()
    {
        return edit->toPlainText();
    }

    QTextEdit* GetTextBox()
    {
        return edit;
    }

private:
    QTextEdit* edit;
    bool toHide;
    bool textToSet;
    QString tempText;
    Qt::Alignment alignment;

    void hiddenNotification()
    {
        paintEnabled = false;
        Hidden();
    }
};

#endif // TEXTBOX_H
